package com.ruoyi.common.security.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.SQLException;
import java.util.List;

/**
 * 类描述:
 *
 * @author 31592
 * @date 2023-06-01 15:23
 */
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public MyTenantLineInnerInterceptor() {
        super();
    }

    public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }

    private static List<String> IGNORE_STATEMENT_NAMES = Lists.newArrayList(
            "com.ruoyi.system.mapper.SysRoleMapper.selectRolePermissionByOrgUserId",
            "com.ruoyi.system.mapper.SysMenuMapper.selectLoginMenuPermsByUserId",
            "com.ruoyi.system.mapper.SysOrgMapper.*",
            "com.ruoyi.system.mapper.SysUserMapper.selectUserByUserNameAll",
            "com.ruoyi.system.mapper.SysOrgCertificateMapper.*",
            "com.ruoyi.lims.mapper.TemplateTableMapper.selectAllTemplateList",
            "com.ruoyi.lims.mapper.StoreJclbMapper.deleteOrgJclb",
            "com.ruoyi.lims.mapper.StoreJclbMapper.selectOrgJclbList",
            "com.ruoyi.lims.mapper.StoreJclbMapper.deleteJclbProperty",
            "com.ruoyi.lims.mapper.StoreJclbMapper.deleteJclbProperty",
            "com.ruoyi.lims.mapper.StoreMethodSampleMapper.deleteSampleByIsSyn",
            "com.ruoyi.lims.mapper.StoreMethodJcxmPriceMapper.selectStoreMethodJcxmPriceList",
            "com.ruoyi.lims.mapper.StoreFixerMapper.selectStoreFixerList",
            "com.ruoyi.lims.mapper.StoreFixerMapper.deleteFixer",
            "com.ruoyi.lims.mapper.StoreFixerMapper.deleteFixerJcxm",
            "com.ruoyi.lims.mapper.StoreFixerMapper.selectStoreFixerJcxmList2"
    );

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        String mapperName = ms.getId();
        // 如果statementid存在，则忽略方法
        boolean ignore = false;
        for (String ignoreMapperName : IGNORE_STATEMENT_NAMES) {
            if (ignoreMapperName.endsWith(".*") && mapperName.startsWith(ignoreMapperName.substring(0, ignoreMapperName.length() - 1))) {
                ignore = true;
                break;
            } else if (ignoreMapperName.equals(mapperName)) {
                ignore = true;
                break;
            }
        }
        if (ignore) return;
        super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }
}
